package com.kongyz.app;


import com.kongyz.client.RpcClient;
import com.kongyz.registry.ServiceDiscovery;
import com.kongyz.rpc.HelloService;

public class Benchmark {

    public static void main(String[] args) throws InterruptedException {
        ServiceDiscovery serviceDiscovery=new ServiceDiscovery("10.25.2.128:2181");
        final RpcClient rpcClient=new RpcClient(serviceDiscovery);

        int threadNum=10;
        final int requestNum=100;
        Thread[] threads=new Thread[threadNum];

        long startTime=System.currentTimeMillis();

        for (int i=0;i<threadNum;i++){
            threads[i]=new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i=0;i<requestNum;i++){
                        final HelloService client=rpcClient.create(HelloService.class);
                        String result=client.hello(Integer.toString(i));
                        System.out.println("返回结果："+result);
                    }
                }
            });
            threads[i].start();
        }
        for (int i = 0; i < threads.length; i++) {
            threads[i].join();
        }
        long timeCost = (System.currentTimeMillis() - startTime);
        String msg = String.format("Sync call total-time-cost:%sms, req/s=%s", timeCost, ((double) (requestNum * threadNum)) / timeCost * 1000);
        System.out.println(msg);

        rpcClient.stop();
    }
}
